<?php

namespace app\common\controller;

use app\common\logic\CacheKey;
use think\Db;

/**
 * 前台 - 基类
 * Class Front
 * @package app\common\controller
 */
class Front extends Base
{

    /**
     * 初始化
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _initialize()
    {
        parent::_initialize();

        $this->setThemes();

        $this->uid = is_login();
        $this->assign('uid', $this->uid);

        //导航
        $nav = $this->getChannelList();
        $this->assign('top_nav', $nav['top']);
        $this->assign('bottom_nav', $nav['bottom']);

        //友链
//        $link = $this->getLinkList();
        $this->assign('link_friend', []);
        $this->assign('link_cooper', []);

        if ($this->uid > 0) {
            $this->assign('unread', Db::name('user_msg')->where(['uid' => $this->uid, 'status' => 0])->count());
        }

    }

    /**
     * 取得导航列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getChannelList()
    {
        $topNav = cache('index_top_nav') ?: [];
        $bottomNav = cache('index_bottom_nav') ?: [];

        if (empty($topNav) || empty($topNav)) {

            $nav = model('Channel')->field('id,title,type,url,target')
                ->where(['status' => 1])
                ->order('sort')
                ->select();
            $nav = collection($nav)->toArray();

            foreach ($nav as $k => $v) {

                $v['url'] = get_nav_url($v['url']);

                if ($v['type'] == 1) {
                    array_push($topNav, $v);
                }

                if ($v['type'] == 2) {
                    array_push($bottomNav, $v);
                }
            }

            cache('index_top_nav', $topNav);
            cache('index_bottom_nav', $bottomNav);
        }

        return [
            'top' => $topNav,
            'bottom' => $bottomNav
        ];
    }

    /**
     * 友链
     * @return array
     */
//    public function getLinkList()
//    {
//        $linkFriend = cache(CacheKey::LINK_FRIEND) ?: [];
//        $linkCooper = cache(CacheKey::LINK_COOPER) ?: [];
//
//        if (empty($linkFriend) || empty($linkCooper)) {
//
//
//            $nav = db('link')
//                ->field('id,title,ftype,url,cover_id')
//                ->where(['status' => 1])
//                ->select();
//
//            $nav = collection($nav)->toArray();
//
//            foreach ($nav as $k => $v) {
//                if ($v['ftype'] == 1) {
//                    array_push($linkFriend, $v);
//                }
//                if ($v['ftype'] == 2) {
//                    array_push($linkCooper, $v);
//                }
//            }
//
//            cache(CacheKey::LINK_FRIEND, $linkFriend);
//            cache(CacheKey::LINK_COOPER, $linkCooper);
//        }
//
//        return [
//            'friend' => $linkFriend,
//            'cooper' => $linkCooper
//        ];
//    }

    /**
     *  设置主题信息
     */
    public function setThemes()
    {
        $themes['mobile'] = config('mobile_themes') ? config('mobile_themes') : 'mobile';
        $themes['pc'] = config('pc_themes') ? config('pc_themes') : 'default';
        $theme = ($this->isMobile() && config('open_mobile_site') == '1') ? $themes['mobile'] : $themes['pc'];
        $module = $this->request->module();


//        if ($module == 'index') {
//            $view_path = ROOT_PATH . 'template' . DS . $theme . DS;
//        } else {
//        $view_path = ROOT_PATH . 'template' . DS . $theme . DS . $module . DS;
//        }

//        $tpl_replace = array(
//            '__TPL__' => $view_path,
//            '__CSS__' => url('/') . 'static/css',
//            '__JS__' => BASE_PATH . 'static/js',
//            '__IMG__' => BASE_PATH . 'static/images',
//        );

//        $tpl_conf = array(
//            'view_path' => $view_path,
//            'tpl_replace_string' => $tpl_replace
//        );

        $theme_path = $this->isMobile() ? 'm' : 'pc';

        // 视图输出字符串内容替换
        $this->view->config([
            'view_path' => ROOT_PATH . 'template' . DS . $theme . DS . $module . DS,
            'tpl_replace_string' => [
                '__CSS__' => url('/', '', false, true) . 'static/' . $theme_path . '/css',
                '__IMG__' => url('/', '', false, true) . 'static/' . $theme_path . '/images',
                '__JS__' => url('/', '', false, true) . 'static/' . $theme_path . '/js',
                '__PUBLIC__' => url('/', '', false, true) . 'static/common'
            ],
        ]);


//        $this->view->config($tpl_conf);

    }
}